<?php
class userModel extends model
{
    public function checklogin($policenumber,$password){
        $password = md5($password);
        $user = $this->dao->select('*')->from(TABLE_USERS)->where('police_number')->eq($policenumber)->andWhere('password')->eq($password)->fetch();
        $this->session->set('user', $user);
        $this->app->user = $this->session->user;
        return $user;
    }
    public function login($policenumber,$password){
        $user = $this->checklogin($policenumber,$password);
        if($user){
            $data = array(
                'result'=>'success',
            );
        }else{
            $data = array(
                'result'=>'fail',
                'message'=>'警号或者密码错误',
            );
        }
        return json_encode($data);
    }

    function checkpsd($value,$minLen=6,$maxLen=18){
        $match='/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';
        $v = trim($value);
        if(empty($v))
            return false;
        return preg_match($match,$v);
    }
    public function changepsd($originalPassword,$password){
        if($originalPassword==""){
            $data = array(
                'erro'=>'0',
                'data'=>$this->lang->user->empryoriginal
            );
            return $data;
        }
        $user = $_SESSION['user'];
        $policenumber = $user->police_number;
        $originalPassword = md5($originalPassword);
        $istrue = $this->dao->select('id')->from(TABLE_USERS)->where('police_number')->eq($policenumber)->andWhere('password')->eq($originalPassword)->fetch();
        if(!$istrue){
            $data = array(
                'erro'=>'0',
                'data'=>$this->lang->user->checkpsd
            );
            return $data;
        }
        $checkpsd = $this->checkpsd($password,6,18);
        if(!$checkpsd){
            $data = array(
                'erro'=>'0',
                'data'=>$this->lang->user->nospecification
            );
            return $data;
        }
        $password = md5($password);
        $this->dao->update(TABLE_USERS)
            ->set('password')->eq($password)
            ->where('police_number')->eq($policenumber)
            ->exec();
        if(!dao::isError()){
            $data = array(
                'erro'=>'1',
                'data'=>$this->lang->user->editsucess
            );
            return $data;
        }else{
            $data = array(
                'erro'=>'0',
                'data'=>$this->lang->user->editfail
            );
            return $data;
        }

    }

    public function isLogon()
    {
        return (isset($_SESSION['user']) and !empty($_SESSION['user']));
    }
    public function getlang(){
        return $this->lang->user->yearsList;
    }

    public function getprocess(){

        $category = $this->dao->select('*')->from(TABLE_BIGCATEGORY)->orderBy('id asc')->fetchAll();
        foreach($category as $val){
            $process = $this->dao->select('*')->from(TABLE_PROCESS)
                ->where('bigcategory')->like('%%'.$val->id.'%%')
                ->andWhere('istrue')->eq('yes')
                ->andWhere('approval_status')->eq('final')
                ->andWhere('revocation')->ne('yes')
                ->orderBy('id desc')->limit(4)->fetchAll();
            foreach($process as $pre){
                $person = $this->dao->select('name')->from(TABLE_USERS)->where('id_card')->eq($pre->personnel)->fetch();
                $pre->personnel = $person->name;
                $type =  $this->dao->select('mistake')->from(TABLE_CATEGORY)->where('id')->in($pre->category)->fetchPairs();
                $pre->category = implode($type,',');

            }
            $val->process = $process;
        }

        return $category;
    }



}